package com.xiguaji.tiktok.v1.web.ins;

import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.brand.dto.InsBrandResp;
import com.xiguaji.tiktok.v1.ins.brand.dto.InsBrandSearchRes;
import com.xiguaji.tiktok.v1.ins.brand.model.CommonBrand;
import com.xiguaji.tiktok.v1.ins.brand.service.InsBrandService;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.product.dto.PlatformTypeEnum;
import com.xiguaji.tiktok.v1.product.dto.ReportTypeEnum;
import com.xiguaji.tiktok.v1.product.service.ProductService;
import com.xiguaji.tiktok.v1.user.dto.UserReturnDto;
import com.xiguaji.tiktok.v1.web.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class InsBrandController extends InsBaseController{

    @Autowired
    InsBrandService insBrandService;
    @Autowired
    ProductService productService;
    @Autowired
    InsFavoriteService insFavoriteService;

    /**
     * 热门广告贴子搜索
     * @return
     */
    @RequestMapping(value = "/brand-search",method = RequestMethod.GET)
    public ModelAndView search(){
        ModelAndView modelAndView = getModel(Tab.INS_BRAND.getValue());
        InsBrandSearchRes insBrandSearchRes = new InsBrandSearchRes();

        Page<InsBrandResp> insBrandRespPage = insBrandService.getPage(insBrandSearchRes);
        if(null != getUser()){

            insBrandRespPage.getList().forEach( brand ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BRAND.getCode(),brand.getId(),(int)getUser().getId());

                if(null != insFavorite) {
                    brand.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insBrandRespPage);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.setViewName("ins/brand/index");
        return modelAndView;
    }

    @RequestMapping(value = "/brand/{brandName}",method = RequestMethod.GET)
    public ModelAndView brandDetail(@PathVariable String brandName){
        ModelAndView modelAndView = getModel(Tab.INS_BRAND.getValue());
        CommonBrand commonBrand = insBrandService.getBrandByName(brandName);
        modelAndView.addObject("brand",commonBrand);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        boolean isUnlock = false;
        Integer collectId = null;
        UserReturnDto userReturnDto = getUserReturn();

        if(commonBrand.getId() != null && commonBrand.getId() > 0 && null != userReturnDto) {
            InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BRAND.getCode(),commonBrand.getId(),(int)getUser().getId());

            if(null != insFavorite) {
                collectId = insFavorite.getId();
            }

            isUnlock = productService.isUnlocked(getUser().getId(), commonBrand.getId(), PlatformTypeEnum.INS.getCode());
            // 未解锁并是豪华版以上的用户自动解锁
            long roleId = getRoleId();
            if (isUnlock == false) {
                // 角色剩余次数
                int unlockCount = productService.unlockedNumberByRole(userReturnDto.getId(), roleId, ReportTypeEnum.BRAND.getMsg());
                // 报告次数
                if (unlockCount < 1) {
                    unlockCount = productService.unlockedNumberByReportStarter(userReturnDto.getId());
                }
                // 角色解锁
                if (unlockCount > 0) {
                    isUnlock = productService.unlockedByUserRole(userReturnDto.getId(), roleId, commonBrand.getId(), PlatformTypeEnum.INS.getCode(), ReportTypeEnum.BRAND.getCode(), ReportTypeEnum.BRAND.getMsg());
                    modelAndView.addObject("user", userReturnDto);
                }
            }
        }
        modelAndView.addObject("collectId",collectId);
        modelAndView.addObject("isUnlocked",isUnlock);
        modelAndView.setViewName("ins/brand/detail/index");
        return modelAndView;
    }
}
